package chapter_23.manager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Administrator
 * Created time 2018/8/10 10:13.
 * 线程终止(通过结束变量 例如: command )
 */
public class ThreadStop {

    private static String command = "";

    public static void main(String[] args) {

        // 创建线程t1,参数是一个线程执行对象 Runner
        Thread thread = new Thread(() -> {

            // 线程执行开始
            System.out.println("线程执行开始...");

            // 一直循环,直到满足条件再终止线程
            while (!command.equalsIgnoreCase("exit")) {
                // 线程开始工作
                System.out.println("下载中...");
                try {
                    // 线程休眠
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    System.out.println("线程执行异常!");
                }
            }

            // 线程执行结束
            System.out.println("线程执行结束...");
        });

        // 开始线程 thread
        thread.start();

        try (InputStreamReader isr = new InputStreamReader(System.in);
             BufferedReader bf = new BufferedReader(isr)
        ) {
            // 从键盘接收一个字符串的输入
            command = bf.readLine();
            System.out.println(command);
        } catch (IOException e) {
            System.out.println("IOException 异常!");
        }
    }

}
